package databank;


import guest.GuestVo;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;

public class DataBank {
	
	//첨부 파일
	int fileSize = 10*1024*1024;
	String uploadPath = "C:\\kjm_1403\\web1403\\WebContent\\upload";
	String fileEncoding = "utf-8";
	
	
	String driver = "oracle.jdbc.driver.OracleDriver";
	String url = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
	String db_user = "hr";
	String db_pwd = "hr";

	Connection conn;
	DataBankVo vo;
	
	

	public DataBank(){
		try {
			// 1) 드라이버 로딩
			Class.forName(driver).newInstance();

			// 2) Connection 객체 생성
			conn = DriverManager.getConnection(url, db_user, db_pwd);

			// 3) PreparedStatement 작성
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	public Vector<DataBankVo> filelist(){
		Vector<DataBankVo> v = new Vector<>();   
		String sql = "select * from databank where mid like ? or subject like ? or content like ? order by mdate" ;
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, "%"+ vo.getFind() +"%");
			ps.setString(2, "%"+ vo.getFind() +"%");
			ps.setString(3, "%"+ vo.getFind() +"%");
			ResultSet rs = ps.executeQuery();
			
			while(rs.next()){
				
				DataBankVo dv = new DataBankVo();
				dv.setSerial(rs.getInt("serial"));
				dv.setMid(rs.getString("mid"));
				dv.setSubject(rs.getString("subject"));
				dv.setMdate(rs.getDate("mdate"));
				
				v.add(dv);
			}
			
			rs.close();
			ps.close();
			conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return v;
	}
	public DataBankVo view(){
		DataBankVo dbv = null;
		String sql = "select * from databank where serial=?";
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, vo.getSerial());
			ResultSet rs = ps.executeQuery();
			
			if(rs.next()){
				dbv = new DataBankVo();
				
				dbv.setSerial(rs.getInt("serial"));
				dbv.setMid(rs.getString("mid"));
				dbv.setMdate(rs.getDate("mdate"));
				dbv.setPwd(rs.getString("pwd"));
				dbv.setSubject(rs.getString("subject"));
				dbv.setContent(rs.getString("content"));
				dbv.setFilename(rs.getString("filename"));
				
			}
			rs.close();
			ps.close();
			conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return dbv;
	}
	public boolean delete(){
		boolean flag = false;
		String sql = "delete from databank where serial=?";
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, vo.getSerial());
			
			int cnt = ps.executeUpdate();
			if(cnt >0){
				flag = true;
				File file = new File(getUploadPath() + "/" + vo.getFilename());
				if(file.exists()){
					file.delete();
				}else{
					//작업중
				}
				
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return flag;
	}
	public boolean fileDel(){
		boolean flag = false;
		String sql = "update databank set filename='' where serial=?";
		try{
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, vo.getSerial());
			
			int cnt = ps.executeUpdate();
			if(cnt >0){
				flag = true;
				File file = new File(getUploadPath() + "/" + vo.getFilename());
				if(file.exists()){
					file.delete();
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean insert(){
		boolean flag=false;
		try{
			MultipartRequest mr = new MultipartRequest(
					vo.getRequest(),
					uploadPath,
					fileSize, 
					fileEncoding, 
					new DefaultFileRenamePolicy()
			);
			String mid = mr.getParameter("mid");
			String subject = mr.getParameter("subject");
			String content = mr.getParameter("content");
			String pwd = mr.getParameter("pwd");
			
			
			String orifile = mr.getOriginalFileName("filename");
			String sysfile = mr.getFilesystemName("filename");
			
			
			
			String sql = "insert into databank values(seq_databank.nextval,?,sysdate,?,?,?,?)";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, mid);
			ps.setString(2, pwd);
			ps.setString(3, subject);
			ps.setString(4, content);
			ps.setString(5, sysfile);
		
			
			int cnt = ps.executeUpdate();
			if(cnt>0) flag=true;
			
			System.out.println("mid : " + mid);
			System.out.println("ori : " + orifile);
			System.out.println("sys : " + sysfile);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return flag;
	} 
	
	public boolean modify(){
		boolean flag = false;
		try{
			MultipartRequest mr = new MultipartRequest(
					vo.getRequest(),
					uploadPath,
					fileSize, 
					fileEncoding, 
					new DefaultFileRenamePolicy()
			);
			String subject = mr.getParameter("subject");
			String content = mr.getParameter("content");
			String pwd = mr.getParameter("pwd");
			String oldfile = mr.getParameter("filename");
			String sysfile = mr.getFilesystemName("newfilename");
			int serial = Integer.parseInt(mr.getParameter("serial"));
			
			//데이터 베이스 수정
			PreparedStatement ps = null;
			String sql = null;
			int cnt=0;
			
			if(oldfile == null || oldfile == ""){
				// 첨부파일이 새로이 추가된 경우
				if(sysfile != null && sysfile != ""){
					
					sql = "update databank set subject=?, content=?, filename=? where serial=? and pwd=?";
					ps = conn.prepareStatement(sql);
					ps.setString(1, subject);
					ps.setString(2, content);
					ps.setString(3, sysfile);
					ps.setInt(4, serial);
					ps.setString(5, pwd);
					
					cnt = ps.executeUpdate();
					if(cnt>0) flag = true;
				}else{
					// 첨부파일 수정없이 내용만 수정된 경우
					sql = "update databank set subject=?, content=? where serial=? and pwd=?";
					ps = conn.prepareStatement(sql);
					ps.setString(1, subject);
					ps.setString(2, content);
					ps.setInt(3, serial);
					ps.setString(4, pwd);
					
					cnt = ps.executeUpdate();
					if(cnt>0) flag = true;
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return flag;
	}

	
	public DataBankVo getVo() {
		return vo;
	}

	public void setVo(DataBankVo vo) {
		this.vo = vo;
	}

	public String getUploadPath() {
		return uploadPath;
	}

	
}
